package com.cgnpc.scp.run.service.impl;

import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.exception.ScpException;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.run.mybatis.entity.*;
import com.cgnpc.scp.run.mybatis.mapper.client.*;
import com.cgnpc.scp.run.service.ProjectImplementationService;
import com.cgnpc.scp.run.service.ProjectInfoService;
import com.cgnpc.scp.run.service.ProjectUndertakeService;
import com.cgnpc.scp.run.utils.CommonRunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("projectImplementationService")
public class ProjectImplementationServiceImpl implements ProjectImplementationService {

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private ProjectUndertakeService projectUndertakeService;

    @Autowired
    private ProjectInfoService projectInfoService;

    @Autowired
    private ProjectImplementationTeamMapper projectImplementationTeamMapper;

    @Autowired
    private ProjectImplementationShuxingMapper projectImplementationShuxingMapper;

    @Autowired
    private ProjectImplementationWbsMapper projectImplementationWbsMapper;

    @Autowired
    private ProjectImplementationSummaryMapper projectImplementationSummaryMapper;

    @Autowired
    private ProjectImplementationImplementMapper projectImplementationImplementMapper;

    @Autowired
    private ProjectImplementationSummaryInfoMapper projectImplementationSummaryInfoMapper;

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Override
    public AjaxResult team(ProjectImplementationTeam projectImplementationTeam) {
        String projectInfoId = projectImplementationTeam.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_implementation_team";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectImplementationTeamMapper.insert((ProjectImplementationTeam) CommonRunUtil.saveOrUpdateData(projectImplementationTeam, currentUser));
            projectUndertakeService.updateProjectStage(projectInfoId);
        }else{
            projectImplementationTeam.setId(stageId);
            projectImplementationTeamMapper.updateById(
                    (ProjectImplementationTeam) CommonRunUtil.saveOrUpdateData(projectImplementationTeam, currentUser)
            );
        }
        List<ProjectMember> members = projectImplementationTeam.getMembers();
        if(members != null && members.size() != 0){
            projectUndertakeService.saveOrUpdateMembers(members, stageTableName, projectInfoId);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult option(ProjectImplementationShuxing projectImplementationShuxing) {
        String projectInfoId = projectImplementationShuxing.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_implementation_shuxing";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectImplementationShuxingMapper.insert((ProjectImplementationShuxing) CommonRunUtil.saveOrUpdateData(projectImplementationShuxing, currentUser));
            projectUndertakeService.updateProjectStage(projectInfoId);
        }else{
            projectImplementationShuxing.setId(stageId);
            projectImplementationShuxingMapper.updateById(
                    (ProjectImplementationShuxing) CommonRunUtil.saveOrUpdateData(projectImplementationShuxing, currentUser)
            );
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult wbs(ProjectImplementationWbs projectImplementationWbs) {
        String projectInfoId = projectImplementationWbs.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_implementation_wbs";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectImplementationWbsMapper.insert((ProjectImplementationWbs) CommonRunUtil.saveOrUpdateData(projectImplementationWbs, currentUser));
            projectUndertakeService.updateProjectStage(projectInfoId);
        }else{
            projectImplementationWbs.setId(stageId);
            projectImplementationWbsMapper.updateById(
                    (ProjectImplementationWbs) CommonRunUtil.saveOrUpdateData(projectImplementationWbs, currentUser)
            );
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult summary(ProjectImplementationSummary projectImplementationSummary) {
        String projectInfoId = projectImplementationSummary.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_implementation_summary";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectImplementationSummaryMapper.insert((ProjectImplementationSummary) CommonRunUtil.saveOrUpdateData(projectImplementationSummary, currentUser));
            projectUndertakeService.updateProjectStage(projectInfoId);
        }else{
            projectImplementationSummary.setId(stageId);
            projectImplementationSummaryMapper.updateById(
                    (ProjectImplementationSummary) CommonRunUtil.saveOrUpdateData(projectImplementationSummary, currentUser)
            );
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult implement(List<ProjectImplementationImplement> implementList
                                , String projectSummaryRequirement) {
        if(implementList == null || implementList.size() == 0){
            return AjaxResult.error("implementList can not be null");
        }
        String projectId = implementList.get(0).getProjectid();
        projectImplementationImplementMapper.deleteByProjectId(projectId);

        for(ProjectImplementationImplement imp : implementList){
            if(StringUtil.isNullOrSpace(imp.getId())){
                imp.setId(IDHelper.uuid());
            }
            projectImplementationImplementMapper.insert(imp);
        }
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(projectId);
        projectInfo.setProjectSummaryRequirement(projectSummaryRequirement);
        projectInfoMapper.updateById(projectInfo);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult summaryInfo(List<ProjectImplementationSummaryInfo> summaryInfoList) {
        if(summaryInfoList == null || summaryInfoList.size() == 0){
            return AjaxResult.error("implementList can not be null");
        }
        String projectId = summaryInfoList.get(0).getProjectid();
        projectImplementationSummaryInfoMapper.deleteByProjectId(projectId);

        for(ProjectImplementationSummaryInfo sum : summaryInfoList){
            if(StringUtil.isNullOrSpace(sum.getId())){
                sum.setId(IDHelper.uuid());
            }
            projectImplementationSummaryInfoMapper.insert(sum);
        }
        return AjaxResult.success();
    }
}
